import { getUserRegionTree } from '@/utils/LocalDataUtil'
import { getUserGridTree } from '@/utils/SessionDataUtil'

export const getAllGridIdArrByGridTrrData = data => {
  if (data && data.allId) {
    let allId = data.allId.replace('[0],', '')
    let allGridIdArr = allId.split(',')
    if (
      allGridIdArr &&
      allGridIdArr instanceof Array &&
      allGridIdArr.length > 0
    ) {
      let newArr = []
      for (let id of allGridIdArr) {
        if (id !== '0') {
          id = id.replace('[', '').replace(']', '')
          newArr.push(id)
        }
      }
      return newArr
    }
  }
}

export const getCaseAllGridIdByGridTreeData = data => {
  let allGridIdArr = getAllGridIdArrByGridTrrData(data)
  if (
    allGridIdArr &&
    allGridIdArr instanceof Array &&
    allGridIdArr.length > 0
  ) {
    return allGridIdArr.join(',')
  }
}

export const allGridIdStr2CaseValue = data => {
  if (data && data !== '') {
    data = data.replace('[0],', '')
    data = data.replace(/\[/g, '').replace(/\]/g, '')
    return data
  }
}

export const strRemoveSquareBrackets = str => {
  if (str && str.trim !== '') {
    str = str.replace(/\[/g, '').replace(/\]/g, '')
    return str
  } else {
    return null
  }
}

export const getLastIdByAllId = data => {
  if (data) {
    let i = data.lastIndexOf(',')
    if (i && i !== -1) {
      return data.substring(i + 1)
    } else {
      return data
    }
  }
}

// 使用JSON实现深clone
export const deepClone = source => {
  return JSON.parse(JSON.stringify(source))
}

// 原region:getOptionDataType 将tree结构改为caseOptions的结构
export const getCaseOptions = data => {
  let options = []
  for (let i = 0; i < data.length; i++) {
    let obj = {}
    obj.value = data[i]['id']
    if (!data[i]['name']) obj.label = data[i]['agencyName']
    else obj.label = data[i]['name']
    if (data[i].children) {
      obj.children = []
      for (let j = 0; j < data[i].children.length; j++) {
        var cobj = {}
        cobj.value = data[i].children[j]['id']
        cobj.label = data[i].children[j]['name']
        obj.children[j] = cobj
        if (data[i].children[j].children) {
          obj.children[j].children = []
          for (let k = 0; k < data[i].children[j].children.length; k++) {
            var ccobj = {}
            ccobj.value = data[i].children[j].children[k]['id']
            ccobj.label = data[i].children[j].children[k]['name']
            obj.children[j].children[k] = ccobj
          }
        }
      }
    }
    options[i] = obj
  }
  return options
}
/**
 * 从传入的data中找找个id的所有父节点
 * @param {id, options} data case中的对象与id
 */
export const getAllParentIdByOptions = data => {
  if (!data || !data.id || !data.options) {
    return
  }
  let id = data.id
  let regionData = data.options
  let arr = []
  for (let i = 0; i < regionData.length; i++) {
    if (arr.length === 2) break
    if (id === regionData[i].value) {
      arr[0] = regionData[i].value
      break
    }
    if (regionData[i].children) {
      for (let j = 0; j < regionData[i].children.length; j++) {
        if (arr.length === 3) break
        if (id === regionData[i].children[j].value) {
          arr[0] = regionData[i].value
          arr[1] = regionData[i].children[j].value
          break
        }
        if (regionData[i].children[j].children) {
          for (let k = 0; k < regionData[i].children[j].children.length; k++) {
            if (id === regionData[i].children[j].children[k].value) {
              arr[0] = regionData[i].value
              arr[1] = regionData[i].children[j].value
              arr[2] = regionData[i].children[j].children[k].value
              break
            }
          }
        }
      }
    }
  }
  return arr
}

export const getAllParentIdByRegionOptions = data => {
  let treeData = getUserRegionTree()
  if (treeData && treeData instanceof Array) {
    return getAllParentIdByOptions({ id: data, options: treeData[0].children })
  }
}
export function isCommonDo (data, el) {
  el.$set(data, 'isCommonDo', true)
}
/**
 * 根据传入id查找相对应的name并返回
 */
export const getLabelById = id => {
  var treeData = getUserGridTree()
  var data = treeData[0].children
  var label = ''
  for (let i = 0; i < data.length; i++) {
    if (data[i].id === id) {
      label = data[i].name
    } else {
      if (data[i].children) {
        for (let j = 0; j < data[i].children.length; j++) {
          if (data[i].children[j].id === id) {
            label = data[i].children[j].name
          } else {
            if (data[i].children[j].children) {
              for (let k = 0; k < data[i].children[j].children.length; k++) {
                if (data[i].children[j].children[k].id === id) { label = data[i].children[j].children[k].name }
                if (label !== '') break
              }
            }
          }
          if (label !== '') break
        }
      }
    }
    if (label !== '') break
  }
  return label
}
